package com.bootcamp.servlet.cart;

/**
 * @program: wrok-1101
 * @author: Keyu Li
 * @create: 2021-11-04 19:49
 **/

import com.bootcamp.servlet.mvc.model.Product;
import com.bootcamp.servlet.util.AuthLoginUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ShopStoreServlet", value = "/ShopStoreServlet", loadOnStartup = 1)
public class ShopStoreServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        // 最上方一栏：返回主页，显示购物车，显示历史足迹
        String homepage = request.getContextPath();
        String cart = request.getContextPath() + "/ShowCartServlet";
        String footprint = request.getContextPath() + "/ShowFootPrintServlet";
        // 判断登录状态
        AuthLoginUtils.authLoginStatus(request,writer);
        // 写入首页
        writer.println("<div><a href='"
                + homepage + "'>返回主页</a>&nbsp;&nbsp;&nbsp;<a href='"
                + cart + "'>购物车</a>&nbsp;&nbsp;&nbsp;<a href='"
                + footprint + "'>历史足迹</a></div>");

        // 展示商品
        List<Product> products = (List<Product>) getServletContext().getAttribute("products");
        writer.println("<div><table>");
        // 第一行
        writer.println("        <tr style=\"font-size: larger\">\n" +
                "            <td>品牌</td>\n" +
                "            <td>颜色</td>\n" +
                "            <td>类别</td>\n" +
                "            <td>价格</td>\n" +
                "            <td>描述</td>\n" +
                "            <td></td>\n" +
                "        </tr>");
        // 内容
        for (Product p : products) {
            String href = request.getContextPath() + "/GoodsDetailServlet?id=" + p.getId();
            writer.println("<tr>\n" +
                    "                <td>" + p.getName() + "</td>\n" +
                    "                <td>" + p.getColor() + "</td>\n" +
                    "                <td>" + p.getCategory() + "</td>\n" +
                    "                <td>" + p.getPrice() + "</td>\n" +
                    "                <td>" + p.getDescription() + "</td>\n" +
                    "                <td><a href='" + href + "'>详情</a></td>\n" +
                    "        </tr>");
        }
        writer.println("</table></div>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
